﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.ComponentModel;
using System.Collections;
using Infrastructure.Aspect.Extensions;
using System.Windows.Controls.Primitives;
namespace Infrastructure.WpfClient.Controls
{

    public enum PanelCommandNames 
    {
        Add, 
        Update, 
        Delete, 
        Save, 
        Close
    }


    /// <summary>
    /// Interaction logic for MgmtPanelControl.xaml
    /// </summary>
    public partial class MgmtPanelControl : UserControl, INotifyPropertyChanged
    {
        #region Commands
        public ICommand NewCommand
        {
            get { return (ICommand)GetValue(NewCommandProperty); }
            set { SetValue(NewCommandProperty, value); }
        }
        public static readonly DependencyProperty NewCommandProperty = DependencyProperty.Register("NewCommand", typeof(ICommand), typeof(MgmtPanelControl));


        public ICommand CloseCommand
        {
            get { return (ICommand)GetValue(CloseCommandProperty); }
            set { SetValue(CloseCommandProperty, value); }
        }
        public static readonly DependencyProperty CloseCommandProperty = DependencyProperty.Register("CloseCommand", typeof(ICommand), typeof(MgmtPanelControl));


        public ICommand SaveCommand
        {
            get { return (ICommand)GetValue(SaveCommandProperty); }
            set { SetValue(SaveCommandProperty, value); }
        }

        // Using a DependencyProperty as the backing store for SaveCommand.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty SaveCommandProperty = DependencyProperty.Register("SaveCommand", typeof(ICommand), typeof(MgmtPanelControl));




        public ICommand DeleteCommand
        {
            get { return (ICommand)GetValue(DeleteCommandProperty); }
            set { SetValue(DeleteCommandProperty, value); }
        }
        public static readonly DependencyProperty DeleteCommandProperty = DependencyProperty.Register("DeleteCommand", typeof(ICommand), typeof(MgmtPanelControl));



        public object DeleteCommandParameter
        {
            get { return (object)GetValue(DeleteCommandParameterProperty); }
            set { SetValue(DeleteCommandParameterProperty, value); }
        }

        // Using a DependencyProperty as the backing store for DeleteCommandParameter.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty DeleteCommandParameterProperty = DependencyProperty.Register("DeleteCommandParameter", typeof(object), typeof(MgmtPanelControl), new UIPropertyMetadata(null));






        public ICommand SearchCommand
        {
            get { return (ICommand)GetValue(SearchCommandProperty); }
            set { SetValue(SearchCommandProperty, value); }
        }
        public static readonly DependencyProperty SearchCommandProperty = DependencyProperty.Register("SearchCommand", typeof(ICommand), typeof(MgmtPanelControl));



        public ICommand PreviusCommand
        {
            get { return (ICommand)GetValue(PreviusCommandProperty); }
            set { SetValue(PreviusCommandProperty, value); }
        }
        public static readonly DependencyProperty PreviusCommandProperty = DependencyProperty.Register("PreviusCommand", typeof(ICommand), typeof(MgmtPanelControl));



        public ICommand NextCommand
        {
            get { return (ICommand)GetValue(NextCommandProperty); }
            set { SetValue(NextCommandProperty, value); }
        }
        public static readonly DependencyProperty NextCommandProperty = DependencyProperty.Register("NextCommand", typeof(ICommand), typeof(MgmtPanelControl));


        public ICommand UpdateCommand
        {
            get { return (ICommand)GetValue(UpdateCommandProperty); }
            set { SetValue(UpdateCommandProperty, value); }
        }
        public static readonly DependencyProperty UpdateCommandProperty = DependencyProperty.Register("UpdateCommand", typeof(ICommand), typeof(MgmtPanelControl));


        #endregion
        
        #region  properties
        public Visibility ShowNew
        {
            get { return (Visibility)GetValue(ShowNewProperty); }
            set { SetValue(ShowNewProperty, value); }
        }
        public static readonly DependencyProperty ShowNewProperty = DependencyProperty.Register("ShowNew", typeof(Visibility), typeof(MgmtPanelControl));

        public Visibility ShowDelete
        {
            get { return (Visibility)GetValue(ShowDeleteProperty); }
            set { SetValue(ShowDeleteProperty, value); }
        }
        public static readonly DependencyProperty ShowDeleteProperty = DependencyProperty.Register("ShowDelete", typeof(Visibility), typeof(MgmtPanelControl));

        public Visibility ShowSave
        {
            get { return (Visibility)GetValue(ShowSaveProperty); }
            set { SetValue(ShowSaveProperty, value); }
        }
        public static readonly DependencyProperty ShowSaveProperty = DependencyProperty.Register("ShowSave", typeof(Visibility), typeof(MgmtPanelControl));


        


        
        public Visibility ShowSearch
        {
            get { return (Visibility)GetValue(ShowSearchProperty); }
            set { SetValue(ShowSearchProperty, value); }
        }
        public static readonly DependencyProperty ShowSearchProperty = DependencyProperty.Register("ShowSearch", typeof(Visibility), typeof(MgmtPanelControl));


        public Visibility ShowNavigation
        {
            get { return (Visibility)GetValue(ShowNavigationProperty); }
            set { SetValue(ShowNavigationProperty, value); }
        }
        public static readonly DependencyProperty ShowNavigationProperty = DependencyProperty.Register("ShowNavigation", typeof(Visibility), typeof(MgmtPanelControl));


        public Visibility ShowClose
        {
            get { return (Visibility)GetValue(ShowCloseProperty); }
            set { SetValue(ShowCloseProperty, value); }
        }
        public static readonly DependencyProperty ShowCloseProperty = DependencyProperty.Register("ShowClose", typeof(Visibility), typeof(MgmtPanelControl));


        public Visibility ShowUpdate
        {
            get { return (Visibility)GetValue(ShowUpdateProperty); }
            set { SetValue(ShowUpdateProperty, value); }
        }
        public static readonly DependencyProperty ShowUpdateProperty = DependencyProperty.Register("ShowUpdate", typeof(Visibility), typeof(MgmtPanelControl));


        public bool ShowConfirmDelete
        {
            get { return (bool)GetValue(ShowCorfirmDeleteProperty); }
            set { SetValue(ShowCorfirmDeleteProperty, value); }
        }
        public static readonly DependencyProperty ShowCorfirmDeleteProperty = DependencyProperty.Register("ShowConfirmDelete", typeof(bool), typeof(MgmtPanelControl));



        public ICollectionView CurrentCollection
        {
            get { return (ICollectionView)GetValue(CurrentCollectionProperty); }
            set { SetValue(CurrentCollectionProperty, value); }
        }
        public static readonly DependencyProperty CurrentCollectionProperty;
#endregion 
              

      


        public static readonly RoutedEvent CommandClickEvent = EventManager.RegisterRoutedEvent("CommandClick", RoutingStrategy.Bubble,
            typeof(RoutedPropertyChangedEventHandler<PanelCommandNames>), typeof(MgmtPanelControl));
        public event RoutedPropertyChangedEventHandler<PanelCommandNames> CommandClick
        {
            add { AddHandler(CommandClickEvent, value); }
            remove { RemoveHandler(CommandClickEvent, value); }
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            Button b = e.OriginalSource as Button;            
            var input = (PanelCommandNames)Enum.Parse(typeof(PanelCommandNames), b.Tag.ToString());

            switch (input)
            {
                case PanelCommandNames.Add:
                    break;
                case PanelCommandNames.Update:
                    break;
                case PanelCommandNames.Delete:
                    if (ShowConfirmDelete && this.DeleteCommand != null)
                    {
                        if (this.DeleteCommand.CanExecute(this.DeleteCommandParameter))
                        {
                            if (MessageBox.Show("Do you want delete this item?","Delete item", MessageBoxButton.YesNo) == MessageBoxResult.Yes)
                                this.DeleteCommand.Execute(this.DeleteCommandParameter);
                        }
                    }
                    else if ( this.DeleteCommand != null) 
                    {
                        if (this.DeleteCommand.CanExecute(this.DeleteCommandParameter))
                            this.DeleteCommand.Execute(this.DeleteCommandParameter);
                    }
                    break;
                case PanelCommandNames.Save:
                    break;
                case PanelCommandNames.Close:
                    break;
                default:
                    break;
            }
            RoutedPropertyChangedEventArgs<PanelCommandNames> args = new RoutedPropertyChangedEventArgs<PanelCommandNames>(
                        input, input, CommandClickEvent);
            RaiseEvent(args);
        }

        static MgmtPanelControl()
        {            
              FrameworkPropertyMetadata metadata = new FrameworkPropertyMetadata(new PropertyChangedCallback(CurrentCollectionPropertyChanged));
              CurrentCollectionProperty = DependencyProperty.Register("CurrentCollection", typeof(ICollectionView), typeof(MgmtPanelControl), metadata);            
        }
        private static void CurrentCollectionPropertyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs args)
        {
            var control = (MgmtPanelControl)sender;
            if (control != null)
            {
                var coll = (ICollectionView)args.NewValue;
                if (coll != null)
                {
                    coll.CollectionChanged += delegate(object sen, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
                    {
                        if ( coll.SourceCollection != null)
                            control.txtNumberOfItems.Text = coll.SourceCollection.Count().ToString();
                    };
                    coll.CurrentChanged += delegate(object sen, EventArgs e) {
                        if (coll.SourceCollection != null && coll.CurrentPosition >=0  && coll.CurrentPosition <= coll.SourceCollection.Count())
                        {
                            control.txtPosition.Text = (coll.CurrentPosition + 1).ToString();
                        }                        
                    };                    
                }
            }
        }             
        public MgmtPanelControl()
        {
            this.InitializeComponent();            
        }
        public event PropertyChangedEventHandler PropertyChanged;
        public void InvokePropertyChanged(string propertyName)
        {
            PropertyChangedEventHandler Handler = PropertyChanged;
            if (Handler != null) Handler(this, new PropertyChangedEventArgs(propertyName));
        }

        private void btnNext_Click(object sender, RoutedEventArgs e)
        {
            if (this.CurrentCollection != null && NextCommand == null)
            {
                this.CurrentCollection.MoveCurrentToNext();
            }
        }

        private void btnPrevius_Click(object sender, RoutedEventArgs e)
        {
            if (this.CurrentCollection != null && PreviusCommand == null)
            {
                this.CurrentCollection.MoveCurrentToPrevious();
            }            
        }
    }
}
